<?php
/**
 * 合同
 * @author Administrator
 *
 */
class Application_Model_DbTable_Contract extends Application_Model_DbTable_Base
{
    protected $_name = 'contract';

	/**
	 * 取得列表
	 */
	public function getUserListForFlex($pageM = NULL){
		$rows = $this->jqfetchAll($pageM);
		return $rows;
	}
	
	/**
	 * 统一的添加下拉列表项目
	 * @param Application_Form_Base $formM
	 */
	public static function setMultiOptions(Application_Form_Base $formM){
		//添加客户
		$customerE = $formM->getElement("customer");
		if($customerE){
			$customerM = new Application_Model_DbTable_Customer();
			$customerL = $customerM->fetchAll(" `type` IN (0,1) ");
			$customerD = array();
			foreach ($customerL as $customer){
				$customerD[$customer["id"]] = $customer["name"] ;
			}
			$customerE->setMultiOptions($customerD);
		}
		
		//添加办事员
		$clerkE = $formM->getElement("clerk");
		if($clerkE){
			$clerkM = new Application_Model_DbTable_User();
			$clerkL = $clerkM->fetchAll();
			$clerkD = array();
			foreach ($clerkL as $clerk){
				$clerkD[$clerk["id"]] = $clerk["showname"] ;
			}
			$clerkE->setMultiOptions($clerkD);
		}
		
		//添加货币
		$currencyE = $formM->getElement("currency");
		if($currencyE){
			$currencyM = new Application_Model_DbTable_Currency();
			$currencyL = $currencyM->fetchAll();
			$currencyD = array();
			foreach ($currencyL as $currency){
				$currencyD[$currency["id"]] = $currency["name"]."(".$currency["abbreviation"].")";
			}
			$currencyE->setMultiOptions($currencyD);
		}
				
		//添加产品
		$product_idE = $formM->getElement("product_id");
		if($product_idE){
			$product_idM = new Application_Model_DbTable_Product();
			$product_idL = $product_idM->fetchAll();
			$product_idD = array();
			foreach ($product_idL as $product_id){
				$product_idD[$product_id["id"]] = $product_id["name_cn"];
			}
			$product_idE->setMultiOptions($product_idD);
		}
		
		//添加国家和地区
		$countryE = $formM->getElement("country");
		if($countryE){
			$countryM = new Application_Model_DbTable_Country();
			$countryL = $countryM->fetchAll();
			$countryD = array();
			foreach ($countryL as $country){
				$countryD[$country["id"]] = $country["name_cn"];
			}
			$countryE->setMultiOptions($countryD);
		}
		
		//港口
		$port_of_shipmontE = $formM->getElement("port_of_shipmont");
		if($port_of_shipmontE){
			$port_of_shipmontM = new Application_Model_DbTable_Port();
			$port_of_shipmontL = $port_of_shipmontM->getKvIdNameCn();
			$port_of_shipmontE->setMultiOptions($port_of_shipmontL);
		}
		$destinationE = $formM->getElement("destination");
		if($destinationE){
			$destinationM = new Application_Model_DbTable_Port();
			$destinationL = $destinationM->getKvIdNameCn();
			$destinationE->setMultiOptions($destinationL);
		}
		$port_ransshipmentE = $formM->getElement("port_ransshipment");
		if($port_ransshipmentE){
			$port_ransshipmentM = new Application_Model_DbTable_Port();
			$port_ransshipmentL = $port_ransshipmentM->getKvIdNameCn();
			$port_ransshipmentE->setMultiOptions($port_ransshipmentL);
		}
		return $formM;
	}

	/**
	 * 删除临时项目
	 */
	public function deleteTempItem(){
		try{
			$this->delete( array("temp_state" => 1 ));
		}catch (Exception $e){
			
		}
	}
}

